package curl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurlTest {

    public static void main(String[] args) {
        // curl -v --basic -u <appid>:<appsecret> -k http://106.52.3.122:8080/api/v3/brokers
        //String[] cmds={"curl","-v","--basic","-u","51e5f003eec1c:MjkwNjMzMDAxODY2NDExODQ4Mjc0MjM4NjQwMDc0MjYwNDI","-k", "http://106.52.3.122:8080/api/v3/brokers"};
        String[] cmds={"curl","-v","--basic","-u","8fa1adb8dd9ee:MjkyOTU4MTY5ODE1NTA0MDIyNDM4MjQzNjQyODY1MDkwNTG","-k", "http://106.52.3.122:8081/api/v4/brokers"};
        System.out.println("auth:"+execCurl(cmds));
    }

    /**
     *@Author: caoxiabo on 2019/12/17 11:22
     *@Description: get 可以，post 后面的 param参数 不知道怎么写进去
     */
    public static String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();

        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;
    }
}
